package com.platform.novel.api;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.platform.annotation.IgnoreAuth;
import com.platform.entity.CollectEntity;
import com.platform.entity.UserEntity;
import com.platform.service.CollectService;
import com.platform.util.ApiBaseAction;
import com.platform.utils.R;

import io.swagger.annotations.Api;

@Api(tags = "novel_collect")
@RestController
@RequestMapping("/apin/collect")
public class ApiNovelCollectController extends ApiBaseAction{
    @Autowired
	private CollectService collectService;

    /**
               * 查看列表
     */
    @RequestMapping("/list")
    public R list(
    		@RequestParam Map<String, Object> map) {
    	Integer userId = super.getCurrentUserId();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", userId);
        params.put("sidx", "id");
        params.put("order", "desc");
        
        if(map.get("offset") != null) {
            params.put("offset", Integer.valueOf(map.get("offset").toString()));
        }else {
            params.put("offset", 0);
        }
        if(map.get("limit") != null) {
            params.put("limit", Integer.valueOf(map.get("limit").toString()));
        }else {
            params.put("limit", 6);
        }
        
        List<CollectEntity> collectList = collectService.queryListc(params);
        return R.ok().put("collectList", collectList);
    }
	
    /**
 	   * 收藏
	 */
	@RequestMapping("/add/{categoryId}")
	public R add(@PathVariable("categoryId") Integer categoryId) {
		UserEntity currentUser = super.getCurrentUser();
		if(currentUser == null) {
			return R.forceLogin();
		}
		CollectEntity collect = new CollectEntity();
		collect.setUserId(currentUser.getId());
		collect.setValueId(categoryId);
		collect.setTypeId(1);
		collectService.save(collect);
	    return R.ok();
	}
	
    /**
 	    * 取消收藏
	 */
	@RequestMapping("/del/{categoryId}")
	public R del(@PathVariable("categoryId") Integer categoryId) {
		UserEntity currentUser = super.getCurrentUser();
		if(currentUser == null) {
			return R.forceLogin();
		}
		collectService.delByCategoryId(currentUser.getId(),categoryId);
	    return R.ok();
	}
}
